package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Lists;
import com.maiji.cloud.entities.login.UploadRecord;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.service.UploadRecordService;
import com.maiji.cloud.utils.DetailTextUtil;
import com.maiji.cloud.utils.MaijiOSS;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.maiji.cloud.entities.login.ActivityShareRuleConfigEntity;
import com.maiji.cloud.mapper.ActivityShareRuleConfigMapper;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.ActivityShareRuleConfigResData;
import com.maiji.cloud.service.ActivityShareRuleConfigService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class ActivityShareRuleConfigServiceImpl  extends ServiceImpl<ActivityShareRuleConfigMapper, ActivityShareRuleConfigEntity>
        implements ActivityShareRuleConfigService {

    @Autowired
	private ActivityShareRuleConfigMapper activityShareRuleConfigMapper;
    @Autowired
    private UploadRecordService uploadRecordService;

	@Override
	public BaseDataResDto<ActivityShareRuleConfigResData> getActivityShareRuleConfigByType(Integer type) {
		
  	  EntityWrapper entityWrapper = new EntityWrapper();
  	  entityWrapper.eq("type", type);
  	  ActivityShareRuleConfigEntity activityShareRuleConfigEntity =  selectOne(entityWrapper);
  	  
  	  ActivityShareRuleConfigResData activityShareRuleConfigResData = new ActivityShareRuleConfigResData();
  	  
  	  if(activityShareRuleConfigEntity != null) BeanUtils.copyProperties(activityShareRuleConfigEntity, activityShareRuleConfigResData);
		
  	  return new BaseDataResDto<ActivityShareRuleConfigResData>(Status.SUCCESS).setData(activityShareRuleConfigResData);
	}

	@Override
	public BaseResDto add_updActivityShareRuleConfig(ActivityShareRuleConfigEntity shareRuleConfig) throws Exception {
	    String ruleId = StringUtil.isBlank(shareRuleConfig.getUuId()) ? UUID_MD5.getUUID() : shareRuleConfig.getUuId();
        List<UploadRecord> uploadRecords = Lists.newArrayList();
//        String rule = DetailTextUtil.dealDetailText(MaijiOSS.OSSARTICLEBUKET, ruleId, shareRuleConfig.getRule(), uploadRecords);
        String text = DetailTextUtil.dealDetailText(MaijiOSS.OSSARTICLEBUKET, ruleId, shareRuleConfig.getText(), uploadRecords);
        String textTwo = DetailTextUtil.dealDetailText(MaijiOSS.OSSARTICLEBUKET, ruleId, shareRuleConfig.getTextTwo(), uploadRecords);
        if (StringUtil.isBlank(shareRuleConfig.getUuId())) {
            if (! insert(shareRuleConfig.setUuId(ruleId).setText(text).setTextTwo(textTwo))) return new BaseResDto(Status.ERROR);
        } else if (! updateById(shareRuleConfig.setText(text).setTextTwo(textTwo))) return new BaseResDto(Status.ERROR);
        if (uploadRecords.size() > 0 && ! uploadRecordService.insertBatch(uploadRecords)) return new BaseResDto(Status.ERROR);
        List<UploadRecord> imgs = Arrays.asList(shareRuleConfig.getBgImgId(), shareRuleConfig.getCardImgId()).parallelStream().map(imgId ->
                new UploadRecord().setUuId(imgId).setBelongId(ruleId)).collect(Collectors.toList());
        if (! uploadRecordService.updateBatchById(imgs)) return new BaseResDto(Status.ERROR);
        return new BaseResDto(Status.SUCCESS);
	}

}
